# -*- coding: utf-8 -*-
import scrapy
from scrapy.spiders.crawl import CrawlSpider,Rule
from scrapy.linkextractors import LinkExtractor

from imagedownload.items import ImagedownloadItem


class ZcoolSpider(CrawlSpider):
    name = 'zcool'
    allowed_domains = ['zcool.com.cn']
    start_urls = ['https://www.zcool.com.cn/discover/0!0!0!0!0!!!!2!0!1']

    rules = (
        Rule(LinkExtractor(allow=r".+/0!0!0!0!0!!!!2!0!\d+"),follow=True),
        Rule(LinkExtractor(allow=r".+/work/.+html"),callback='parse_detail',follow=False),
    )

    def parse_detail(self, response):
        image_urls = response.xpath("//div[contains(@class,'work-show-box')]//img/@src").getall()
        title_list =  response.xpath("//div[contains(@class,'details-contitle-box')]/h2/text()").getall()
        title = ''.join(title_list).strip()
        item = ImagedownloadItem(image_urls=image_urls,title=title)
        yield item

